import { ModalForm, ProFormSelect } from '@ant-design/pro-components';
import { Button, message } from 'antd';
import { copyCashierSchedule, getCashierNames } from '../service';
import { CopyScheduleType } from '../types.d';

const CopySchedule = (props: {
  showSchduleDay: string;
  jobNo: string;
  userName: string;
  ownerId: string;
}) => {
  const { jobNo, userName, showSchduleDay, ownerId } = props;
  return (
    <ModalForm<CopyScheduleType>
      title={`将员工${userName}自${showSchduleDay}其至月底的排班复制至指定用户`}
      trigger={
        <Button type="primary" style={{ marginLeft: '8px' }}>
          复制排班
        </Button>
      }
      layout="horizontal"
      width="40vw"
      labelCol={{ span: 6 }}
      wrapperCol={{ span: 14 }}
      modalProps={{
        destroyOnClose: true,
        maskClosable: false,
      }}
      onFinish={async (values) => {
        message.loading({ content: '正在操作，请稍后', key: 'copy_schedule' });
        const data2copy: CopyScheduleType = {
          jobNo,
          jobNos: values.jobNos,
          ownerId,
          day: showSchduleDay,
        };
        const res = await copyCashierSchedule(data2copy);
        if (!res?.success) {
          message.error({ content: res?.message, key: 'copy_schedule' });
          return false;
        }
        message.success({ content: res?.message, key: 'copy_schedule' });
        return true;
      }}
    >
      <ProFormSelect
        name="jobNos"
        label="选择员工"
        showSearch
        mode="multiple"
        debounceTime={300}
        placeholder="请选择要复制当前排班的员工"
        rules={[
          {
            required: true,
            validator: (_, val) => {
              if (!val || val.length < 1) {
                return Promise.reject(new Error('要复制当前排班的员工不可为空'));
              }
              if (val && val.length <= 10) {
                return Promise.resolve(val);
              }
              return Promise.reject(new Error('要复制当前排班的员工最多选择10个'));
            },
          },
        ]}
        request={(val) => getCashierNames(val.keyWords, ownerId, jobNo)}
      />
    </ModalForm>
  );
};

export default CopySchedule;
